package com.axjy.util;

import com.axjy.pojo.BasePojo;
import com.axjy.pojo.User;
import org.springframework.aop.MethodBeforeAdvice;

import javax.servlet.http.HttpSession;
import java.lang.reflect.Method;
import java.time.LocalDateTime;

/**
 * Created by l on 2018/1/10.
 */
public class BasePojoAdvice implements MethodBeforeAdvice {
    @Override
    public void before(Method method, Object[] objects, Object o) throws Throwable {
        BasePojo basePojo=null;
        if(objects!=null&&objects[0]!=null&&objects[0] instanceof BasePojo){
            basePojo=(BasePojo) objects[0];
            basePojo.setCreationDate(LocalDateTime.now());
        }
        if(objects!=null&&objects[1]!=null&&objects[1] instanceof HttpSession){
            HttpSession session=(HttpSession) objects[1];
            User loginUser=(User) session.getAttribute("loginUser");
            basePojo.setCreatedBy(loginUser.getCreatedBy());
        }
    }
}
